home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dskut / restart3.zip / RESTART.C < prev    next >
C/C++ Source or Header  |  1990-09-24  |  9KB  |  389 lines

  1. /* restart program (V2.0) by Tom Almy, May 1990 */
  2. /* improved version (V2.1) by Thomas Birnthaler, Aug 1990 */
  3. /* still more improved version (V2.2) by Tom Almy, Sept 1990 */
  4. /* contributed to the public domain */
  5. /* compile with Turbo C using the options -1- -f- -G- -O -lt -mt -p */
  6. /* compile with Microsoft C v6.0 with -AT -G0rs -Osleazr  */
  7. /* This will generate minimum size modules */
  8.  
  9. #define VERSION "restart (V2.2 24.9.90)"
  10.  
  11. #ifdef __TURBOC__
  12. #define _cdecl cdecl
  13. #define _far far
  14. #endif
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <ctype.h>
  19. #include <string.h>
  20. #include <time.h>
  21.  
  22.  
  23. /* constant definitions */
  24. #define  SEPSTR      "'"
  25. #define  SEPCH       '\''
  26. #define  NEGCH       '~'
  27. #define  FILECH      '*'
  28.  
  29. #define  BOOTDRIVE   "BOOTDRIVE"
  30. #define  COMSPEC     "COMSPEC"
  31. #define  CONFIGFILE  "c:\\configur.dat"
  32.  
  33. #define  READONLY    "rt"
  34. #define  WRITEONLY   "wt"
  35.  
  36. #define  MAXLINELEN  256   
  37. #define  MAXDOSLINELEN 126  /* DOS can't handle more */
  38.  
  39. #define  MAXCONF     20   /* maximum configuration arguments */
  40. #define  MAXCONFS    "20"
  41.  
  42. #define  ON   1
  43. #define  OFF  0
  44.  
  45. #define  USAGE    1
  46. #define  COMMENT  1
  47.  
  48. /* variable declarations */
  49. int PREVIEW = OFF;
  50. int REBOOT  = ON;
  51. int DELAY   = 0;
  52. int COLD  = OFF;
  53.  
  54. char *progname;
  55. char infile[64] = CONFIGFILE;
  56. char outfile[64];
  57.  
  58. FILE *input;
  59. FILE *output = stdout;
  60.  
  61. char linebuf[MAXLINELEN];
  62.  
  63. int  confcnt = 0;
  64. char *confname[MAXCONF];        /* selected restart config's */
  65.  
  66.  
  67. /* function declarations */
  68. void version(void);
  69. void openconfigfile(void);
  70. void getargs(int argc, char *argv[]);
  71. int getline(void);
  72. void trim(char *line);
  73. void sendline(char *cp);
  74. void printline(void);
  75. void setargfromline(void);
  76. void sleep(int delay);
  77. void boot(void);
  78. void error(char *string1, char *string2, int USAGEFLAG, int COMMENTFLAG);
  79. void usage(void);
  80. void showcomment(void);
  81.  
  82.  
  83. void _cdecl main(int argc, char *argv[])
  84. {
  85.   char *cp;
  86.   int  nr;
  87.  
  88.   version();
  89.   openconfigfile();
  90.   getargs(argc,argv);
  91.  
  92.   if (getline() == EOF)
  93.     error("empty input file %s", infile, !USAGE, !COMMENT);
  94.  
  95.   if (linebuf[0] != SEPCH ||
  96.       linebuf[strlen(linebuf)-1] != SEPCH ||
  97.       strlen(linebuf) <= 2)
  98.     error("wrong syntax in config line %s", linebuf, !USAGE, !COMMENT);
  99.  
  100.  
  101.   for (nr=0; nr < confcnt; ++nr) {
  102.     if (!strstr(linebuf,confname[nr]))
  103.       error("config name %s not found in config file", confname[nr],
  104.             !USAGE, COMMENT);
  105.   }
  106.  
  107.   while (linebuf[0] != FILECH) {
  108.     if (getline() == EOF)
  109.       error("didn't find *filename line in config file %s", infile,
  110.             !USAGE, !COMMENT);
  111.  
  112.     /* defining new argument? */
  113.     if (linebuf[0] == SEPCH) {
  114.       for (nr=0; nr < confcnt; ++nr) {
  115.         if (strstr(linebuf, confname[nr]) != NULL) {
  116.           setargfromline();
  117.           break;
  118.         }
  119.       }
  120.     }
  121.     if (linebuf[0] == NEGCH) {
  122.       for (nr=0; nr < confcnt; ++nr) {
  123.         if (strstr(linebuf, confname[nr]) != NULL) break;
  124.       }
  125.       if (nr == confcnt) /* none matched */
  126.         setargfromline();
  127.     }
  128.     
  129.   }
  130.  
  131.   while (linebuf[0] == FILECH) {
  132.  
  133.     if (PREVIEW)
  134.       printf("\n%s:\n",linebuf+1);
  135.     else
  136.       if((output = fopen(linebuf+1, WRITEONLY)) == NULL)
  137.         error("cannot write file %s", linebuf+1, !USAGE, !COMMENT);
  138.  
  139.     while (getline() != EOF && linebuf[0] != FILECH) {
  140.  
  141.       /* config name dependent part */
  142.       if (linebuf[0] == SEPCH) {
  143.         for (nr=0; nr < confcnt; ++nr) {
  144.           if (strstr(linebuf,confname[nr]) != NULL) {
  145.             printline();  /* one matched */
  146.             break;
  147.           }
  148.         }
  149.       }
  150.  
  151.       /* inverted config name dependent part */
  152.       else if (linebuf[0] == NEGCH) {
  153.         for (nr=0; nr < confcnt; ++nr) {
  154.           if (strstr(linebuf,confname[nr]) != NULL)
  155.             break;
  156.         }
  157.         if (nr == confcnt) /* none matched */
  158.           printline();
  159.       }
  160.  
  161.       /* undependent part */
  162.       else
  163.         sendline(linebuf);
  164.     }
  165.  
  166.     if (PREVIEW == OFF)
  167.       fclose(output);
  168.   }
  169.  
  170.   if (REBOOT == ON)  {
  171.     if (DELAY > 0) sleep(DELAY);
  172.     boot();
  173.   }
  174. }
  175.  
  176.  
  177. /* trick system into reboot */
  178. /* For warm (soft) reboot, first store 1234 into location 40:72 */
  179. /* Perform a far call to the reboot location ffff:0000 */
  180. void boot(void)
  181. {
  182.   if (COLD == OFF) *((unsigned int _far *) 0x00400072) = 0x1234;
  183.   ((void (_far *)(void)) 0xffff0000)();
  184. }
  185.  
  186.  
  187. /* remove leading and trailing white space (includes '\n')from line */
  188. void trim(char *line)
  189. {
  190.   char *s;
  191.   char *d;
  192.  
  193.   s = d = line;
  194.   while (isspace(*s)) ++s;
  195.   while (*s) *d++ = *s++;
  196.   do {
  197.     *d-- = '\0';
  198.   } while (d >= line && isspace(*d));
  199. }
  200.  
  201.  
  202. /* give error message and exit */
  203. void error(char *string1, char *string2, int USAGEFLAG, int COMMENTFLAG)
  204. {
  205.   char c;
  206.  
  207.   fprintf(stderr,"error: ");
  208.   fprintf(stderr,string1,string2);
  209.   fprintf(stderr,"\n\n");
  210.   if (USAGEFLAG) usage();
  211.   if (COMMENTFLAG) showcomment();
  212.   exit(1);
  213. }
  214.  
  215.  
  216. /* keep idle for delay seconds */
  217. void sleep(int delay)
  218. {
  219.   time_t endtime;
  220.  
  221.   endtime = time(NULL) + delay;
  222.   while (time(NULL) < endtime) 
  223.     ;
  224. }
  225.  
  226.  
  227. /* get next nonempty line of input, return it's length or EOF */
  228. int getline(void)
  229. {
  230.   int i;
  231.  
  232.   do {
  233.     if (fgets(linebuf,MAXLINELEN,input) == NULL) {
  234.       *linebuf = '\0';
  235.       return(EOF);
  236.     }
  237.     trim(linebuf);
  238.   } while ((i = strlen(linebuf)) == 0);
  239.  
  240.   if (i == MAXLINELEN-1)
  241.     error("line too long: %s", linebuf, !USAGE, !COMMENT);
  242.  
  243.   return(i);
  244. }
  245.  
  246.  
  247. /* send a line to the file, checking for being too long */
  248. void sendline(char *cp)
  249. {
  250.   if (strlen(cp) > MAXDOSLINELEN) 
  251.     error("line too long for DOS: %s", cp, !USAGE, !COMMENT);
  252.   fprintf(output,"%s\n",cp);
  253. }
  254.  
  255.  
  256. /* output line without leading config names */
  257. void printline(void)
  258. {
  259.   char *cp;
  260.   
  261.   cp = linebuf;
  262.   while (*cp && !isspace(*cp)) ++cp;
  263.   while (isspace(*cp)) ++cp;
  264.   if (*cp)
  265.     sendline(cp);
  266. }
  267.   
  268. /* string in line sets another argument as if in command line */
  269. void setargfromline(void)
  270. {
  271.   char *cp, *cp2;
  272.   cp = linebuf;
  273.   while (*cp && !isspace(*cp)) ++cp;
  274.   while (isspace(*cp)) ++cp;
  275.   cp2 = cp;
  276.   while (*cp2 && !isspace(*cp2)) ++cp2;
  277.   *cp2 = 0;
  278.   if (*cp) {
  279.     if (confcnt >= MAXCONF) 
  280.       error("Too many configurations (>%s)", MAXCONFS, USAGE, COMMENT);
  281.     confname[confcnt] = (char *) malloc(strlen(cp) + 3);
  282.     strcpy(confname[confcnt],SEPSTR);
  283.     strcat(confname[confcnt],cp);
  284.     strcat(confname[confcnt],SEPSTR);
  285.     ++confcnt;
  286.   }
  287. }
  288.       
  289.  
  290. void getargs(int argc, char *argv[])
  291. {
  292.   char *cp;
  293.  
  294.   /* reduce to base name */
  295.   progname = *argv;
  296.   if ((cp = strrchr(progname,'\\')) != NULL) progname = cp+1;
  297.   if ((cp = strrchr(progname,'/')) != NULL) progname = cp+1;
  298.   if ((cp = strchr(progname,'.')) != NULL) *cp = '\0';
  299.   for (cp = progname; *cp; ++cp) *cp = tolower(*cp);
  300.   ++argv;
  301.   --argc;
  302.  
  303.   /* get options */
  304.   while (*argv[0] == '-') {
  305.  
  306.     if (strcmp(*argv,"-p") == 0) {
  307.       PREVIEW = ON;
  308.       REBOOT = OFF;
  309.     }
  310.  
  311.     else if (strcmp(*argv,"-n") == 0) {
  312.       REBOOT = OFF;
  313.     }
  314.  
  315.     else if (strcmp(*argv,"-c") == 0) {
  316.       COLD = ON;
  317.     }
  318.  
  319.     else if (sscanf(*argv+1,"%d",&DELAY) == 1 && DELAY > 0) {
  320.     }
  321.     else
  322.       error("unknown option %s", *argv, USAGE, !COMMENT);
  323.  
  324.     ++argv;
  325.     --argc;
  326.   }
  327.  
  328.   /* get selected config names */
  329.   confcnt = 0;
  330.   while (argc-- > 0) {
  331.     if (confcnt >= MAXCONF) 
  332.       error("Too many configurations (>%s)", MAXCONFS, USAGE, COMMENT);
  333.     confname[confcnt] = (char *) malloc(strlen(*argv) + 3);
  334.     strcpy(confname[confcnt],SEPSTR);
  335.     strcat(confname[confcnt],*argv++);
  336.     strcat(confname[confcnt],SEPSTR);
  337.     ++confcnt;
  338.   }
  339.  
  340.   if (confcnt == 0) {
  341.     getline();
  342.     error("no config name(s) given", NULL, USAGE, COMMENT);
  343.     getline();
  344.     exit(1);
  345.   }
  346. }
  347.  
  348.  
  349. void openconfigfile(void)
  350. {
  351.   char *cp;
  352.  
  353.   if ((cp = getenv(BOOTDRIVE)) == NULL)
  354.     cp = getenv(COMSPEC);
  355.   infile[0] = cp[0];
  356.  
  357.   if ((input = fopen(infile,READONLY)) == NULL)
  358.     error("config file %s not fo